package com.xiaoge.libraryuse.interface_call_back;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.xiaoge.libraryuse.R;

import java.util.List;

/**
 * Created by guoyzh on 2018/3/20 0020.
 */

public class CallBackAdapter extends BaseAdapter {


    private final List<String> datas;
    private final Context context;
    private OnItemBtnClickListener itemBtnClickListener;

    public CallBackAdapter(Context context, List<String> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_interface_callback, null);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
        viewHolder.btn = (Button) convertView.findViewById(R.id.btn);

        viewHolder.tv.setText(datas.get(position));

        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 接口回调传过去的数据
                itemBtnClickListener.onItemBtnClick(position, datas.get(position), datas);
            }
        });

        return convertView;
    }

    /**设置接口回调**/
    public void setOnItemBtnClickListener(OnItemBtnClickListener itemBtnClickListener) {
        this.itemBtnClickListener = itemBtnClickListener;
    }

    class ViewHolder {
        TextView tv;
        Button btn;
    }

}
